#include "mc_type.h"
#include "global_variable.h"
#include "mc_parameter_M0.h"
#include "FW_interface.h"
#include "myg0025_conf.h"

#define P_DEBUG_RTT_EN              (0)  //Debug RTT Mode, 1:Enable, 0:Disable
#define P_VREF                      (5)  //V 

//Retry
#define RETRY_TIMES                 (0xFF)  //0xff is unlimited retry   
#define RETRY_DELAY_TIME            (10) //unit:500ms

//Interface setting
#define P_CONTROL_DEBUG             (0)                //Modification is prohibited
#define P_CONTROL_VSP               (1)                //Modification is prohibited
#define P_CONTROL_DUTY              (2)                //Modification is prohibited
#define P_CONTROL_MODE              P_CONTROL_DEBUG

//Control mode
#define P_LOOP_SPD                  (0)                //Modification is prohibited
#define P_LOOP_POWER                (1)                //Modification is prohibited
#define P_LOOP_MODE                 P_LOOP_SPD

#define P_FG_FUNC_EN                (0)  //FG function enable
#define P_FG_GAIN                   ((float)(1.5))  
#define P_FG_Clock                  ((float)(1000000))                    //Modification is prohibited
#define P_FG_GAIN_DEC_PARA          (float)(P_FG_Clock/(P_FG_GAIN*1/60.0))//Modification is prohibited

#define P_VSP_STOP                  (0.2) //V
#define P_VSP_START                 (0.5) //V
#define P_VSP_MAX                   (4.5) //V

#define P_PWM_STOP                  (50)//unit:0.1%
#define P_PWM_START                 (120)//unit:0.1%
#define P_PWM_MAX                   (950)//unit:0.1%
#define P_PWM_SPDFACTOR_PARA        ((512*(CMD_MAX_VALUE_SPD_PARA - CMD_MIN_VALUE_SPD_PARA))/(P_PWM_MAX - P_PWM_START))//Modification is prohibited
#define P_PWM_STOP_PARA             P_PWM_STOP                          //Modification is prohibited
#define P_PWM_START_PARA            P_PWM_START                         //Modification is prohibited

//power control
#define P_POWER_IBUS_PU             (5)   //A, VDD/Rshunt/IGAIN  
#define P_POWER_VBUS_PU             (55)  //V, VDD/VGAIN 
#define P_POWER_POWER_PU_GAIN       (2)   //2^x, max is 9.
#define P_POWER_KP                  (0.2)
#define P_POWER_KI                  (0.01)
#define P_POWER_PI_MAX_SPD          (1200)
#define P_POWER_PI_MIN_SPD          (200)

#define P_POWER_FACTOR_PARA         (9-P_POWER_POWER_PU_GAIN)           //2^12 * 2^12 / 32768 ,Modification is prohibited
#define P_POWER_PU_PARA             ((int)(32768<<P_POWER_POWER_PU_GAIN)/P_POWER_IBUS_PU/P_POWER_VBUS_PU)//Modification is prohibited
#define P_POWER_KP_PARA             (P_POWER_KP*32768)                  //Modification is prohibited
#define P_POWER_KI_PARA             (P_POWER_KI*32768)                  //Modification is prohibited    
#define P_POWER_PI_MAX_SPD_PARA     P_POWER_PI_MAX_SPD                  //Modification is prohibited
#define P_POWER_PI_MIN_SPD_PARA     P_POWER_PI_MIN_SPD                  //Modification is prohibited

#define P_POWER_CMD_MAX_VALUE       (5)   //W
#define P_POWER_CMD_MIN_VALUE       (1)   //W
#define P_POWER_CMD_ACC_TIME        (2)   //sec

#define P_VSP_STARTLEVEL_PARA       (4096*P_VSP_START/P_VREF)               //Modification is prohibited
#define P_VSP_STOPLEVEL_PARA        (4096*P_VSP_STOP/P_VREF)                //Modification is prohibited
#define P_POWER_CMD_MAX_VALUE_PARA  (P_POWER_CMD_MAX_VALUE*P_POWER_PU_PARA) //Modification is prohibited
#define P_POWER_CMD_MIN_VALUE_PARA  (P_POWER_CMD_MIN_VALUE*P_POWER_PU_PARA) //Modification is prohibited
#define P_VSP_FACTOR_PARA           (P_VREF*(P_POWER_CMD_MAX_VALUE_PARA-P_POWER_CMD_MIN_VALUE_PARA)/(P_VSP_MAX-P_VSP_START))//Modification is prohibited
#define P_POWER_CMD_DELTA_PARA      ((P_POWER_CMD_MAX_VALUE_PARA-P_POWER_CMD_MIN_VALUE_PARA)/P_POWER_CMD_ACC_TIME/100)//Modification is prohibited
#define P_PWM_POWERFACTOR_PARA      ((512*(P_POWER_CMD_MAX_VALUE_PARA - P_POWER_CMD_MIN_VALUE_PARA))/(P_PWM_MAX - P_PWM_START))//Modification is prohibited

#define P_SPD_CMD_MAX_VALUE_PARA    (CMD_MAX_VALUE_SPD_PARA) //Modification is prohibited
#define P_SPD_CMD_MIN_VALUE_PARA    (CMD_MIN_VALUE_SPD_PARA) //Modification is prohibited
#define P_SPD_VSP_FACTOR_PARA       (P_VREF*(P_SPD_CMD_MAX_VALUE_PARA-P_SPD_CMD_MIN_VALUE_PARA)/(P_VSP_MAX-P_VSP_START))//Modification is prohibited



typedef struct
{
    volatile s32 s32input;
    volatile s32 s32output;
} LPFs;


extern void UserControlMode(void);


#if(P_FG_FUNC_EN)
    extern void FG_Init(void);
    extern void FG_OUT(u8 u8cnt_div, u32 u32temp);
#endif

#if(P_CONTROL_MODE == P_CONTROL_DUTY)
    extern vu16 g_unPWMDuty;
    extern LPFs LPF_1;
    extern void Capture_Init(void);
    extern u16 unCapture_duty_freq(void);
    extern void Duty_Control(void);
    extern void PWMToUserCmd(void);
    extern void PWM_Control(void);
#endif


#if(P_CONTROL_MODE == P_CONTROL_VSP)
    void VSP_Control(void);
    void VSPToUserCmd(void);
#endif

#if(P_LOOP_MODE == P_LOOP_POWER)
    extern STR_PIC gS_Power_PI;
    void Power_ACC(STR_Motor_Def *tS_M);
    void PowerControl(STR_Motor_Def *tS_M);
#endif


extern void MotorRetry(STR_Motor_Def *tS_M);
#define CLEAR_RETRY_CNT()                   gS_Motor.mS_Ctr.ubRetryCntSet = 0;

